#pragma once

#include "rabbitvcs_def.h"
#include <Dolphin/KVersionControlPlugin>

class QProcess;

class RabbitVcsPlugin : public KVersionControlPlugin
{
    Q_OBJECT

public:
    RabbitVcsPlugin(QObject *parent = nullptr);
    ~RabbitVcsPlugin() override;

    QString fileName() const override;
    QString localRepositoryRoot(const QString &directory) const override;
    bool beginRetrieval(const QString &directory) override;
    void endRetrieval() override;
    ItemVersion itemVersion(const KFileItem &item) const override;
    QList<QAction *> versionControlActions(const KFileItemList &items) const override;
    QList<QAction *> outOfVersionControlActions(const KFileItemList &items) const override;

protected:
    void init();
    bool addAction(RabbitVcsType vcs, const char *name, const char *title, const char *icon = nullptr);
    const QString &currentDirectory() const {return currentDir;}
    ItemVersion checkStatus(const QString &filePath) const;
    void setTargetItems(const KFileItemList &items) const;
    void runRabbitVcsCmd(const QString &module);
    void runRabbitVcsCmd(const QString &module, const QStringList &args);
    KVersionControlPlugin::ItemVersion convertItemVersion(const QString &ver) const;
    QList<QAction *> convertActions(QVector<const char *> names, RabbitVcsType vcs = RabbitVcsType::none) const;

Q_SIGNALS:
    void processStarted();
    void processFinished(int exitCode);

protected Q_SLOTS:
    void actionTriggered(RabbitVcsType vcs, const QString &name);

protected:
    mutable RabbitVcsType currentVcs;
    QString currentDir;
    QProcess *cmdProcess;
    mutable QStringList targetItems;
    QMap<RabbitVcsType, QMap<QString, QAction *>> actions;
};
